package org.jgs1904.apachepoidemo.poi;

import org.apache.poi.ss.usermodel.*;

import java.io.*;

/**
 * 读和写WorksBooks
 *
 * @author: 默苍璃
 * @date: 2023-10-09 17:57
 */
public class ReadAndWrite {

    public static void main(String[] args) {
        try (InputStream inp = new FileInputStream("D:\\temp\\workbookread.xls")) {
//InputStream inp = new FileInputStream("workbook.xlsx");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            Row row = sheet.getRow(2);
            Cell cell = row.getCell(3);
            if (cell == null) cell = row.createCell(3);
            cell.setCellType(CellType.STRING);
            cell.setCellValue("a test");
            // Write the output to a file
            try (OutputStream fileOut = new FileOutputStream("D:\\temp\\workbookwrite.xls")) {
                wb.write(fileOut);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
